﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.Profile;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace Continental.UI.ControlPanel
{
    public partial class ChangeProfile : System.Web.UI.Page
    {
        public String message = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                MembershipUser theUser = Membership.GetUser(HttpContext.Current.User.Identity.Name);
                ProfileBase theProfile = ProfileBase.Create(theUser.UserName);

                this.txtEmail.Text = theUser.Email;
                this.txtFullName.Text = theProfile.GetPropertyValue("FullName").ToString();
                this.txtMobile.Text = theProfile.GetPropertyValue("Mobile").ToString();
                this.txtYahoo.Text = theProfile.GetPropertyValue("Yahoo").ToString();
                this.txtSkype.Text = theProfile.GetPropertyValue("Skype").ToString();
                this.txtAddress.Text = theProfile.GetPropertyValue("Address").ToString();
                this.imgAvartar.ImageUrl = "~/ImageData/Avartar/" + theProfile.GetPropertyValue("Avartar").ToString();

            }
        }

        protected void btnChangeProfile_Click(object sender, EventArgs e)
        {
            Page.Validate();
            if (Page.IsValid)
            {
                message = "";
                MembershipUser theUser = Membership.GetUser(HttpContext.Current.User.Identity.Name);
                ProfileBase theProfile = ProfileBase.Create(theUser.UserName);

                theProfile.SetPropertyValue("Address", this.txtAddress.Text.Trim());
                theProfile.SetPropertyValue("Email", this.txtEmail.Text.Trim());
                theProfile.SetPropertyValue("FullName", this.txtFullName.Text.Trim());
                theProfile.SetPropertyValue("Mobile", this.txtMobile.Text.Trim());
                theProfile.SetPropertyValue("Skype", this.txtSkype.Text.Trim());
                theProfile.SetPropertyValue("Yahoo", this.txtYahoo.Text.Trim());
                if (this.fuAvartar.HasFile)
                {
                    String pathAvatars = Server.MapPath("~/");
                    try
                    {
                        String newFileName = DateTime.Now.Ticks.ToString() + ".gif";
                        int thumbWidth = 75;
                        int thumbHeight = 75;

                        System.Drawing.Image oImg = System.Drawing.Image.FromStream(fuAvartar.PostedFile.InputStream);
                        System.Drawing.Image oThumbNail = new Bitmap(thumbWidth, thumbHeight, oImg.PixelFormat);

                        Graphics oGraphic = Graphics.FromImage(oThumbNail);
                        oGraphic.CompositingQuality = CompositingQuality.HighQuality;
                        oGraphic.SmoothingMode = SmoothingMode.HighQuality;
                        oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;

                        Rectangle oRectangle = new Rectangle(0, 0, thumbWidth, thumbHeight);
                        oGraphic.DrawImage(oImg, oRectangle);

                        oThumbNail.Save(pathAvatars + "\\ImageData\\Avartar\\" + newFileName, ImageFormat.Gif);
                        theProfile.SetPropertyValue("Avartar", newFileName);
                        this.imgAvartar.ImageUrl = "~/ImageData/Avartar/" + newFileName;
                        oImg.Dispose();
                    }
                    catch (Exception ex)
                    {
                        Response.Write("Error: " + ex.Message);
                    }
                }
                theProfile.Save();
                message = "thông tin của bạn đã được cập nhật";
            }
        }
    }
}
